package com.woniuxy.utils;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class EmailUtil {
	// 发送者邮箱
	private static final String SENDADDRESS = "1391568183@qq.com";
	// 授权码
	private static final String POP3_CODE = "lggblmtoxewejici";
	private static final String IMAP_CODE = "mqiquctnnsawjicd";

	public static void send(String email, String order_no) {
		try {
			// 设置初始化参数
			Properties properties = new Properties();
			// 认证邮箱
			properties.put("mail.user", SENDADDRESS);
			// 授权码：此处用任何一个授权码都可以
			// 采用不同的授权码时邮箱服务器会使用不同的协议发送邮件
			properties.put("auction.password", POP3_CODE);
			// 是否认证
			properties.put("mail.smtp.auth", true);
			// 协议
			properties.put("mail.transport.protocol", "smtp");
			// 邮箱服务器
			properties.put("mail.stmp.host", "smtp.qq.com");
			// 邮箱服务器端口号
			properties.put("maill.smtp.port", "25");

			// 创建session
			Session session = Session.getDefaultInstance(properties);

			// 创建消息
			Message message = new MimeMessage(session);
			// 发送人
			message.setFrom(new InternetAddress(SENDADDRESS));
			// 收件人
			message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
			// 邮件主题
			message.setSubject("用户激活邮件-WNXY2");

			// 邮件内容
			String email_content = "<html><head></head><body><h1>尊敬的用户由于为及时支付，该" + order_no + "已过期！</h1><h3>";
			// 设置字符编码
			message.setContent(email_content, "text/html;charset=UTF-8");
			// 保存更改
			message.saveChanges();

			// 设置协议并链接到服务器
			Transport transport = session.getTransport("smtp");
			transport.connect(properties.getProperty("mail.stmp.host"), properties.getProperty("mail.user"),
					properties.getProperty("mail.password"));
			// 发送邮件
			transport.sendMessage(message, message.getAllRecipients());
			// transport.send(message);
			// 关闭
			transport.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
